﻿using System;
using System.Diagnostics.Contracts;

namespace DaveSexton.Labs
{
	internal sealed class Stopwatch
	{
		public TimeSpan Elapsed
		{
			get
			{
				var t = time;
				var c = Environment.TickCount;

				Contract.Assume(t <= c);

				return TimeSpan.FromMilliseconds(c - t);
			}
		}

		private int time = Environment.TickCount;

		public void Restart()
		{
			time = Environment.TickCount;
		}

		public static Stopwatch StartNew()
		{
			Contract.Ensures(Contract.Result<Stopwatch>() != null);

			return new Stopwatch();
		}
	}
}